package com.github.catvod.spider.merge;

import com.github.catvod.spider.merge.HZ;
import java.nio.charset.CharsetEncoder;
import java.util.Arrays;
import java.util.HashMap;

/* loaded from: classes.dex */
public class XR {
    private static final char[] h = {',', ';'};
    private static final HashMap<String, String> V0 = new HashMap<>();
    private static final HZ.Tw j = new HZ.Tw();

    public enum HA {
        xhtml(WG.h, 4),
        base(WG.V0, 106),
        extended(WG.j, 2125);

        private int[] BQ;
        private String[] I;
        private String[] W;
        private int[] oC;

        HA(String str, int i) {
            XR.W(this, str, i);
        }

        String Qy(int i) {
            int binarySearch = Arrays.binarySearch(this.BQ, i);
            if (binarySearch < 0) {
                return "";
            }
            String[] strArr = this.W;
            if (binarySearch < strArr.length - 1) {
                int i2 = binarySearch + 1;
                if (this.BQ[i2] == i) {
                    return strArr[i2];
                }
            }
            return strArr[binarySearch];
        }

        int u(String str) {
            int binarySearch = Arrays.binarySearch(this.I, str);
            if (binarySearch >= 0) {
                return this.oC[binarySearch];
            }
            return -1;
        }
    }

    static /* synthetic */ class Tw {
        static final /* synthetic */ int[] h;

        static {
            int[] iArr = new int[yh.values().length];
            h = iArr;
            try {
                iArr[yh.ascii.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                h[yh.utf.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
        }
    }

    enum yh {
        ascii,
        utf,
        fallback;

        static yh h(String str) {
            return str.equals("US-ASCII") ? ascii : str.startsWith("UTF-") ? utf : fallback;
        }
    }

    public static boolean BQ(String str) {
        return HA.extended.u(str) != -1;
    }

    static void I(Appendable appendable, String str, HZ.Tw tw, boolean z, boolean z2, boolean z3) {
        HA I = tw.I();
        CharsetEncoder q = tw.q();
        yh yhVar = tw.q;
        int length = str.length();
        int i = 0;
        boolean z4 = false;
        boolean z5 = false;
        while (i < length) {
            int codePointAt = str.codePointAt(i);
            if (z2) {
                if (ZG.u(codePointAt)) {
                    if ((!z3 || z4) && !z5) {
                        appendable.append(' ');
                        z5 = true;
                    }
                    i += Character.charCount(codePointAt);
                } else {
                    z4 = true;
                    z5 = false;
                }
            }
            if (codePointAt < 65536) {
                char c = (char) codePointAt;
                if (c != '\"') {
                    if (c == '&') {
                        appendable.append("&amp;");
                    } else if (c != '<') {
                        if (c != '>') {
                            if (c != 160) {
                                if (j(yhVar, c, q)) {
                                    appendable.append(c);
                                } else {
                                    V0(appendable, I, codePointAt);
                                }
                            } else if (I != HA.xhtml) {
                                appendable.append("&nbsp;");
                            } else {
                                appendable.append("&#xa0;");
                            }
                        } else if (z) {
                            appendable.append(c);
                        } else {
                            appendable.append("&gt;");
                        }
                    } else if (!z || I == HA.xhtml || tw.Qy() == HZ.Tw.EnumC0010Tw.xml) {
                        appendable.append("&lt;");
                    } else {
                        appendable.append(c);
                    }
                } else if (z) {
                    appendable.append("&quot;");
                } else {
                    appendable.append(c);
                }
            } else {
                String str2 = new String(Character.toChars(codePointAt));
                if (q.canEncode(str2)) {
                    appendable.append(str2);
                } else {
                    V0(appendable, I, codePointAt);
                }
            }
            i += Character.charCount(codePointAt);
        }
    }

    private static void V0(Appendable appendable, HA ha, int i) {
        String Qy = ha.Qy(i);
        if ("".equals(Qy)) {
            appendable.append("&#x").append(Integer.toHexString(i)).append(';');
        } else {
            appendable.append('&').append(Qy).append(';');
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static void W(HA ha, String str, int i) {
        int i2;
        ha.I = new String[i];
        ha.oC = new int[i];
        ha.BQ = new int[i];
        ha.W = new String[i];
        UF uf = new UF(str);
        int i3 = 0;
        while (!uf.PU()) {
            String n = uf.n('=');
            uf.h();
            int parseInt = Integer.parseInt(uf.i4(h), 36);
            char GM = uf.GM();
            uf.h();
            if (GM == ',') {
                i2 = Integer.parseInt(uf.n(';'), 36);
                uf.h();
            } else {
                i2 = -1;
            }
            int parseInt2 = Integer.parseInt(uf.n('&'), 36);
            uf.h();
            ha.I[i3] = n;
            ha.oC[i3] = parseInt;
            ha.BQ[parseInt2] = parseInt;
            ha.W[parseInt2] = n;
            if (i2 != -1) {
                V0.put(n, new String(new int[]{parseInt, i2}, 0, 2));
            }
            i3++;
        }
        R2.q(i3 == i, "Unexpected count of entities loaded");
    }

    private static boolean j(yh yhVar, char c, CharsetEncoder charsetEncoder) {
        int i = Tw.h[yhVar.ordinal()];
        if (i == 1) {
            return c < 128;
        }
        if (i != 2) {
            return charsetEncoder.canEncode(c);
        }
        return true;
    }

    public static boolean oC(String str) {
        return HA.base.u(str) != -1;
    }

    public static int q(String str, int[] iArr) {
        String str2 = V0.get(str);
        if (str2 != null) {
            iArr[0] = str2.codePointAt(0);
            iArr[1] = str2.codePointAt(1);
            return 2;
        }
        int u = HA.extended.u(str);
        if (u == -1) {
            return 0;
        }
        iArr[0] = u;
        return 1;
    }
}
